import { queryCurrentMaintenance, queryMaintenancPlan, queryMaintenancTrack, queryMaintenancMap } from '../services/monitor';

export default {
  namespace: 'monitor',

  state: {
    mapData:[],
    currentMaintenance: [],
    maintenancPlan:{},
    maintenancTrack: [],
    loading: {
      currentMaintenance: true,
      maintenancPlan:true,
      maintenancTrack:true,
    },
  },

  effects: {
    *fetch_current_mainten({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{currentMaintenance:true}
      })
      const response =yield call(queryCurrentMaintenance, payload);
      response &&  (
        yield put({
          type: 'appendCurrentMaintenance',
          payload:response.data.list,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{currentMaintenance:false}
      })
    },

    *fetch_mainten_plan({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{maintenancPlan:true}
      })
      const response =yield call(queryMaintenancPlan, payload);
      response &&  (
        yield put({
          type: 'appendMaintenancPlan',
          payload:response.data,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{maintenancPlan:false}
      })
    },

    *fetch_mainten_track({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{maintenancTrack:true}
      })
      const response =yield call(queryMaintenancTrack, payload);
      response &&  (
        yield put({
          type: 'appendMaintenancTrack',
          payload:response.data.list,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{maintenancTrack:false}
      })
    },

    *fetch_mainten_map({ payload }, { call, put }) {
      const response =yield call(queryMaintenancMap, payload);
      response &&  (
        yield put({
          type: 'appendMapData',
          payload:response.data.list,
        })
      )
    },


  },

  reducers: {
    appendCurrentMaintenance(state, action) {
      return {
        ...state,
        currentMaintenance: action.payload,
      };
    },

    appendMaintenancPlan(state, action) {
      return {
        ...state,
        maintenancPlan: action.payload,
      };
    },

    appendMaintenancTrack(state, action) {
      return {
        ...state,
        maintenancTrack: action.payload,
      };
    },

    appendMapData(state, action) {
      return {
        ...state,
        mapData: action.payload,
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },


    appendClearState(state, action) {
      return {
        ...state,
        mapData:[],
        currentMaintenance: [],
        maintenancPlan:{},
        maintenancTrack: [],
        loading: {
          currentMaintenance: true,
          maintenancPlan:true,
          maintenancTrack:true,
        },
      };
    },
  },
};

